@extends('web.master')
@section('title','摇一摇')
    @section('head')
        <link rel="stylesheet" href="{{asset('statics/css/other.css')}}" />
        @endsection
    @section('content')
        <div class="shake" onclick="search()" id="search"></div>
        @endsection
@section('foot')
    @php
    $app=app('wechat');
    $js=$app->js;
    @endphp
    <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('statics/js/layer/layer_mobile/layer.js')}}"></script>
    <script type="text/javascript" src="{{asset('game/js/jquery.min.js')}}"></script>
    <script>
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        var send_post=0;
        wx.config({!! $js->config(array('onMenuShareQQ', 'onMenuShareWeibo','hideOptionMenu','startSearchBeacons','onSearchBeacons','stopSearchBeacons','getLocation'), false) !!});
        wx.ready(function () {
            wx.startSearchBeacons({
                ticket: "",
                complete: function (argv) {
		   	
                    if(argv.errMsg!="startSearchBeacons:ok"){
                        layer.open({
                            content: '打开手机GPS定位及蓝牙,走到景点告示牌处摇一摇'
                            ,btn: '我知道了'
                        });

                    }
                }
            });
        });
        function search(){
            layer.open({
                type: 2
                ,content: '正在寻找附近的设备'
            });
            wx.onSearchBeacons({
                complete: function (argv) {


                    var arg;

                    if(argv.beacons.length>1){
                        for(var i=0;i<argv.beacons.length-1;i++){
                            if(argv.beacons[i].accuracy<argv.beacons[i+1].accuracy){
                                arg=argv.beacons[i];
                            }else{
                                arg=argv.beacons[i+1];
                            }
                        }
                    }else if(argv.beacons.length==1){
                        arg=argv.beacons[0]
                    }

                    if(arg.major){

                        if(send_post==0){
                            send_post=1;
                            $.post('{{route('ajaxScenic')}}',{major:arg.major,minor:arg.minor},function(data){
                                if(data){
                                    window.location.href='/scenic/'+data;
                                }else{
                                    send_post=0;
                                    layer.closeAll();
                                    layer.open({
                                        content: '未找到景点'
                                        ,btn: '我知道了'
                                    });
                                }
                            }).error(function() {
                                layer.closeAll();
                                layer.open({
                                    content: '未找到景点'
                                    ,btn: '我知道了'
                                });
                            })

                        }
                    }
                    //回调函数
                }
            });
        }
        function stop(){
            wx.stopSearchBeacons({
                complete:function(res){
                    //关闭查找完成后的回调函数
                    alert(JSON.stringify(res));
                }
            });
        }
        wx.error(function (res) {
            alert(res);
        });

        </script>
    @endsection
